using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Xml;
using Macaw.Wss3.DependencySpiderSolution.FeatureCode;
using System.Reflection;

namespace Macaw.Wss3.DependencySpiderSolution
{
    class DependencyQuickAccessButtonFeatureReceiver : SPFeatureReceiver
    {
        private string EDITINGMENUFOLDER = "/_catalogs/masterpage/Editing Menu/";
        private string MANIFESTNAME_XML = "Macaw.Wss3.DependencySpiderSolution.FeatureCode.DependenciesQuickAccessButton.xml";
        private string CUSTOMFILENAME = "DependenciesQuickAccessButton";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {            
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            SPFolder editingmenufolder = site.RootWeb.GetFolder( EDITINGMENUFOLDER );
            
            Stream dqabstream = this.GetType().Assembly.GetManifestResourceStream( MANIFESTNAME_XML );
            SPFile dqabfile = editingmenufolder.Files.Add( string.Format( "{0}.xml" , CUSTOMFILENAME ) , dqabstream , true);
            dqabfile.CheckIn("Added by DependencySpiderSolution");
            try
            {
                dqabfile.Publish("Added by DependencySpiderSolution");
            }
            catch { }

            SPFile file = site.RootWeb.GetFile(string.Format("{0}CustomQuickAccess.xml", EDITINGMENUFOLDER));

            Stream stream = file.OpenBinaryStream();
            XmlDocument document = new XmlDocument();
            document.Load(stream);
            stream.Close();
            
            XmlNode consolenode = document["Console"];
            if (consolenode == null)
            {
                consolenode = document.AppendChild(document.CreateElement("Console"));
            }
            if (consolenode != null)
            {
                XmlNode customfilenode = consolenode.SelectSingleNode(string.Format("/Console/customfile[@FileName='{0}']", CUSTOMFILENAME));
                if (customfilenode == null)
                {
                    customfilenode = consolenode.AppendChild(document.CreateElement("customfile"));

                    XmlAttribute filenameattribute = customfilenode.Attributes.Append(document.CreateAttribute("FileName"));
                    filenameattribute.Value = CUSTOMFILENAME;
                }
            }

            stream = new System.IO.MemoryStream();
            StreamWriter writer = new StreamWriter(stream, Encoding.ASCII);
            document.Save(writer);
            writer.Flush();

            file.CheckOut();
            
            file.SaveBinary(stream);

            file.CheckIn("Added DependencyQuickAccessButton");
            try
            {
                file.Publish("Added DependencyQuickAccessButton");
            }
            catch { }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = (SPSite)properties.Feature.Parent;

            #region Restore "CustomQuickAccess.xml"
            SPFile file = site.RootWeb.GetFile( string.Format("{0}CustomQuickAccess.xml",EDITINGMENUFOLDER));

            Stream stream = file.OpenBinaryStream();
            XmlDocument document = new XmlDocument();
            document.Load(stream);
            stream.Close();


            XmlNode customfilenode = document.SelectSingleNode(string.Format("/Console/customfile[@FileName='{0}']",CUSTOMFILENAME));
            if (customfilenode != null)
            {
                customfilenode.ParentNode.RemoveChild(customfilenode);
            }

            stream = new System.IO.MemoryStream();
            StreamWriter writer = new StreamWriter(stream, Encoding.ASCII);
            document.Save(writer);

            file.CheckOut();
            file.SaveBinary(stream);

            file.CheckIn("Removed DependencyQuickAccessButton");
            try
            {
                file.Publish("Removed DependencyQuickAccessButton");
            }
            catch { }
            #endregion

            #region Delete "DependenciesQuickAccessButton.xml"
            SPFile dqabfile = site.RootWeb.GetFile(string.Format("{0}{1}.xml",EDITINGMENUFOLDER,CUSTOMFILENAME));
            if (dqabfile != null && dqabfile.Exists)
            {
                dqabfile.Delete();
            }
            #endregion

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
